package 链表与双指针.m03.d21.t05;

import 链表与双指针.ListNode;

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode() {}
 *     ListNode(int val) { this.val = val; }
 *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 * }
 */
class Solution {
    public ListNode removeNthFromEnd(ListNode head, int n) {
        ListNode node = new ListNode();
        node.next = head;
        if(head.next == null) {
            return null;
        }
        ListNode fast = node, cur = node;
        for(int i = 0; i<n; i++) {
            fast = fast.next;
        }
        while(fast != null && fast.next != null) {
            fast = fast.next;
            cur = cur.next;
        }
        cur.next = cur.next.next;
        return node.next;
    }
}